package com.aifast.crm.customer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aifast.common.annotations.AiIdempotent;
import com.aifast.common.base.Controller;

import com.aifast.common.base.R;
import com.aifast.common.layui.LayuiDataTable;
import com.aifast.crm.customer.dao.CrmCustomerDao;
import com.aifast.crm.customer.entity.CrmCustomer;
import com.aifast.crm.customer.service.CrmCustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 客户信息 控制层
 *
 * @author fujian
 * @date 2022-08-24
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/crm/customer")
public class CrmCustomerController extends Controller {

    private final CrmCustomerService service;
    private final CrmCustomerDao dao;

    @GetMapping("test")
    public R test(){
        List<CrmCustomer> list = dao.list(null);
        return OK(list.size());
    }

    @SaCheckPermission("customer:list")
    @GetMapping("list")
    public LayuiDataTable list(CrmCustomer crmCustomer) {
        return service.list(crmCustomer, getPage());
    }

    @AiIdempotent
    @PostMapping("add")
    public R add(@Valid CrmCustomer crmCustomer) {
        return service.insertOrUpdate(crmCustomer) ? OK() : ERROR();
    }

    @AiIdempotent
    @GetMapping("del")
    public R del() {
        return service.del(getLong("id"), get("ids")) ? OK() : ERROR();
    }

    @GetMapping("info")
    public R info() {
        return service.info(getLong("id"));
    }

    @GetMapping("getMembers")
    public LayuiDataTable getMembers() {
        List<Map<String, Object>> members = service.getTeamMember(getInt("customerId"));
        LayuiDataTable build = new LayuiDataTable<>(0, "", members, members.size());
        return build;
    }

    @PostMapping("addMember")
    public R addMember() {
        return service.addTeamMember(getInt("customerId"), get("userIds")) ? OK() : ERROR();
    }

    @GetMapping("delMember")
    public R delMember() {
        return service.delTeamMember(getInt("customerId"), get("userIds")) ? OK() : ERROR();
    }

    @GetMapping("export")
    public void export(CrmCustomer crmCustomer){
        service.export(crmCustomer,response);
    }



}
